﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApC3DAttachmentNameGenerator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EMPAPPLICATIONLib;
using EMPTYPELIBRARYLib;
using EngineeringInternalExtension;
using System;
using System.Globalization;
using System.IO;
using System.Text;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands;

internal class CApC3DAttachmentNameGenerator
{
  private static int maxLenghtOfDataSetNameTC = (int) TxTcUtilities.TcObjectNameMaxLength();
  private static int maxLenghtOfDataSetNameEMS = 251;
  private static string dateFormat = "'_'ddMMyyyy'_'HHmm";
  private static string folderName = "3D Documentation";
  private static string animationFileNamePrefix = "Views";
  private static string nodesFileNamePrefix = "Nodes";

  internal CApC3DAttachmentNameGenerator()
  {
  }

  internal string GenerateAnimationFileName(
    ITxPlanningObject obj,
    string fileExtension,
    bool fullpath = true)
  {
    string path1 = "";
    if (fullpath)
      path1 = this.GetFilePath(obj);
    string path2 = Path.ChangeExtension(this.GetFileName(CApC3DAttachmentNameGenerator.animationFileNamePrefix + "_", obj), fileExtension);
    return Path.Combine(path1, path2);
  }

  internal string GenerateDataFileName(ITxPlanningObject obj)
  {
    return Path.Combine(this.GetFilePath(obj), Path.ChangeExtension(this.GetFileName(CApC3DAttachmentNameGenerator.animationFileNamePrefix + "_", obj), CApC3DUtils.DataFileExtension));
  }

  internal string GenerateNodesFileName(ITxPlanningObject obj)
  {
    return Path.Combine(this.GetFilePath(obj), Path.ChangeExtension(CApC3DAttachmentNameGenerator.nodesFileNamePrefix + DateTime.Now.ToString(CApC3DAttachmentNameGenerator.dateFormat, (IFormatProvider) DateTimeFormatInfo.InvariantInfo), CApC3DUtils.DataFileExtension));
  }

  private string GetFileName(string prefix, ITxPlanningObject obj)
  {
    string name = ((ITxObject) obj).Name;
    string fileName = this.RemoveSystemCharactersFromFileName(this.RemoveNonAsciiCharacters(prefix + name)) + DateTime.Now.ToString(CApC3DAttachmentNameGenerator.dateFormat, (IFormatProvider) DateTimeFormatInfo.InvariantInfo);
    int startIndex = CApC3DAttachmentNameGenerator.maxLenghtOfDataSetNameTC;
    if (TxApplication.PlatformType == 1)
      startIndex = CApC3DAttachmentNameGenerator.maxLenghtOfDataSetNameEMS;
    if (fileName.Length > startIndex)
      fileName = fileName.Remove(startIndex, fileName.Length - startIndex);
    return fileName;
  }

  private string GetFilePath(ITxPlanningObject obj)
  {
    string filePath = "";
    if (TxApplication.PlatformType == 1)
    {
      filePath = Path.Combine(Path.Combine(Path.Combine(TxApplication.SystemRootDirectory, CApC3DAttachmentNameGenerator.folderName), this.GetNodeExternalId(obj)), this.GetVersionId());
      if (!Directory.Exists(filePath))
        this.CreateDirectory(filePath);
      if (this.IsCreatedDirectoryReadOnly(filePath))
        throw new ApplicationException(string.Format(CApC3DUtils.GetResourceManager().GetString("CREATE_3D_DOCUMENTATION_FILE_CREATION_FAILED"), (object) filePath));
    }
    else if (TxTcUtilities.IsTcAnyPlatform)
      filePath = Path.GetTempPath();
    return filePath;
  }

  private string RemoveSystemCharactersFromFileName(string fileName)
  {
    char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
    int index = 0;
    for (int length = invalidFileNameChars.Length; index < length; ++index)
      fileName = fileName.Replace(invalidFileNameChars[index], '_');
    return fileName;
  }

  private string RemoveNonAsciiCharacters(string fileName)
  {
    return Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(fileName));
  }

  private string RemoveSystemCharactersFromFilePath(string filePath)
  {
    char[] invalidPathChars = Path.GetInvalidPathChars();
    int index = 0;
    for (int length = invalidPathChars.Length; index < length; ++index)
      filePath = filePath.Replace(invalidPathChars[index], '_');
    return filePath;
  }

  private string GetNodeExternalId(ITxPlanningObject selectedObject)
  {
    return this.RemoveSystemCharactersFromFilePath(selectedObject.ProcessModelId.ExternalId);
  }

  private string GetVersionId()
  {
    EmpVersionKey empVersionKey1;
    EmpVersionKey empVersionKey2;
    ((IEmpApplication) new EmpApplicationClass()).GetCurrentConfigSpec(ref empVersionKey1, ref empVersionKey2);
    return "version_" + empVersionKey2.versionId.ToString();
  }

  private bool IsCreatedDirectoryReadOnly(string directoryName)
  {
    bool flag = false;
    try
    {
      DirectoryInfo directoryInfo = new DirectoryInfo(directoryName);
      string str = directoryName + "\\tmpDir";
      directoryInfo.CreateSubdirectory("tmpDir").Delete(true);
    }
    catch (UnauthorizedAccessException ex)
    {
      flag = true;
    }
    return flag;
  }

  private void CreateDirectory(string directoryName)
  {
    try
    {
      Directory.CreateDirectory(directoryName);
    }
    catch (Exception ex)
    {
      throw new ApplicationException(string.Format(CApC3DUtils.GetResourceManager().GetString("CREATE_3D_DOCUMENTATION_PERMISSION_ERROR"), (object) directoryName), ex);
    }
  }
}
